<?php
/** * Json_request Class * 
 * @package TIAP 
 * @subpackage Controller 
 * @category json 
 * @author Ibnu Triyono 
 * @description  handling json request
 * @link http://twitter.com/ibnutri 
 */ 
class Json_request extends Controller{
  function Json_request(){
    parent::Controller();
  }
  function index(){
  }
  
    function network_polygon(){
        $this->load->model('Network_model');
        $network_id  = $this->uri->segment(3);
        if($network_id != ''){
            $newest_network_list = $this->Network_model->get_network_polygon($network_id);
            echo json_encode($newest_network_list);
        }else{
        //$newest_network_list = $this->Network_model->get_network_polygon('',10);
            $newest_network_list = $this->Network_model->get_network_and_total_member();
            echo json_encode($newest_network_list);
        }
    }

    function update_directory_photo(){
        $this->load->model('Directory_photo_model');
        $caption = $this->input->post('caption');
        $file_name = $this->input->post('file');
        $photo_id = $this->input->post('id');
        $file_ori = $this->input->post('ori');
        
        $this->Directory_photo_model->update_photo($photo_id, $file_name, $caption, $file_ori);

        $data = array(
            'file' => $file_name,
            'caption' => $caption,
            'id' => $photo_id,
            'ori'=>$file_ori,
        );
        echo json_encode($data);
    }
  
    function search_network() {
        $this->load->model('Network_model');
        $network_name = $this->input->post('keyword');    
        $network_result = $this->Network_model->get_network_name($network_name,10);
        echo json_encode($network_result);        
    }
    
    function get_ads(){
        $this->load->model('Landing_ads_model');
        $limit = $this->input->post('items');
        $ads_list = $this->Landing_ads_model->get_landing_ads($limit,0);        
        echo json_encode($ads_list);
    }
    
    function check_company_json(){
        $this->load->model('Directory_company_model');
        $this->load->helper('tiap_helper');
        $company = strtolower(seo_clean($this->input->post('company')));        
        $current = $this->input->post('existing');        
        if (!$this->Directory_company_model->check_available_company($company,$current)){
            $result_company = 'FALSE';
        } else {
            $result_company = 'TRUE';
        }
        //echo json_encode($this->Directory_company_model->db->last_query());
        //die();
        echo json_encode($result_company);
    }
    
    function crime_marker($networkId=false){
        $this->load->model('Crime_report_model'); 
        if ($networkId){
            $this->load->model('Network_model'); 
            $arrayData = $this->Network_model->getDetail($networkId); 
            $this->Crime_report_model->setBoundaryMaps($arrayData['latitude_min'],$arrayData['longitude_min'],
            $arrayData['latitude_max'],$arrayData['longitude_max']);       
        }
        $crime_list = $this->Crime_report_model->get_crime_report();
        echo json_encode($crime_list);        
    }

    function crime_marker_by_month($month=1){
        $this->load->model('Crime_report_model');
        $this->Crime_report_model->set_the_month($month);
        $crime_list = $this->Crime_report_model->get_crime_report();
        echo json_encode($crime_list);     
    }
	
	function get_directory(){
		
		$this->load->model('Directory_company_model');
		$directorylist = $this->Directory_company_model->get_newest_company_json();
		echo json_encode($directorylist);
		
	}
    
    function crime_marker_in_network($networkId=false){
        $this->load->model('Crime_report_model'); 
        $crime_list = $this->Crime_report_model->get_crime_report();
        $this->db->order_by("id", "ASC"); 
        $query = $this->db->get_where('path', array('id_network' => $networkId));
        
        $pathData = $query->result_array();
        $x = 1;
        foreach($pathData AS $path){
            $polyX[] = $path['lat'];
            $polyY[] = $path['lng'];
            $x++;
        }   
        $this->load->model('Network_model');
                 
        foreach($crime_list AS $row){
            //print_r($row);
            if ($this->Network_model->pointInPolygon($row['latitude'],$row['longitude'],$x,$polyX,$polyY)){
                //echo 'ok'.'<br>';
                $rows[] = $row;
            }
        }
        echo json_encode($rows);    
    }
    
    function check_username_json() {
        $this->load->model('Directory_user_model');        
        $username = $this->input->post('username');
        if (!$this->Directory_user_model->find_user($username)){
            $result_username = 'FALSE';
        } else {
            $result_username = 'TRUE';
        }
        echo json_encode($result_username);
    }
    
	function check_directory_email() {
		$this->load->model('Directory_user_model');
		$email = $this->input->post('email');
        if (!$this->Directory_user_model->check_available_email($email)) {
            $result_email = 'FALSE';
        } else {
            $result_email = 'TRUE';
        }
        echo json_encode($result_email);
    }
    
	function search_firstname() {
        $this->load->model('User_model');
        $user_firstname = $this->input->post('keyword');
        $network_id = $this->input->post('network_id');    
        $user_result = $this->User_model->get_user_firstname($user_firstname, $network_id, 10);
        echo json_encode($user_result);        
    }
}

/* End of file json_request.php */
/* Location: ./system/application/controllers/json_request.php */